#include "cvutil.h"
CVUtil::CVUtil()
{

}

CVUtil::~CVUtil(){}

QUrl CVUtil::getBaseUrl(const QString method, QString networkUrl)
{
    CVSession *session = CVSession::get();
    QString sessionId = session->getSessionId();

    QUrl *url = new QUrl();
    url->setScheme("https");
    if (!networkUrl.isEmpty())
    {
        url->setHost(networkUrl);
    }
    else
    {
        CVNetwork *network = session->getCurrentNetwork();
        if (!network->getUrl().isEmpty())
        {
            url->setHost(network->getUrl());
        }
        else
        {
            return *url;
        }
    }

    url->setPath("/pg/cvapi/rest/json");
    url->addEncodedQueryItem("method", method.toLatin1());
    if (!sessionId.isEmpty())
    {
        url->addEncodedQueryItem("auth_token", sessionId.toLatin1());
    }

    return *url;
}

QString CVUtil::prettyTime(const QString &timestamp)
{
    QString friendly = "";
    uint ts = timestamp.toUInt();
    QDateTime dt = QDateTime::currentDateTime();
    uint curTs = dt.toTime_t();
    uint diff = curTs - ts;
    uint d1 = diff / 60;
    uint d2 = d1 / 60;
    uint d3 = d2 / 24;
    uint d4 = d3 / 30;
    uint d5 = d4 / 365;
    if (d1 <= 1)
    {
        friendly = "Just now";
    }
    else if (d1 > 1 && d2 == 0)
    {
        friendly = QString::number(d1) + " minutes ago";
    }
    else if (d2 > 0 && d3 == 0)
    {
        if (d2 == 1)
        {
            friendly = "1 hour ago";
        }
        else
        {
            friendly = QString::number(d2) + " hours ago";
        }
    }
    else if (d3 > 0 && d4 == 0)
    {
        if (d3 == 1)
        {
            friendly = "Yesterday";
        }
        else
        {
            friendly = QString::number(d3) + " days ago";
        }
    }
    else if (d4 > 0 && d5 == 0)
    {
        if (d4 == 1)
        {
            friendly = "1 month ago";
        }
        else
        {
            friendly = QString::number(d4) + " months ago";
        }
    }
    else if (d5 > 0)
    {
        if (d5 == 1)
        {
            friendly = " 1 year ago";
        }
        else
        {
            friendly = QString::number(d5) + " years ago";
        }
    }
    else
    {
        QDateTime dt1 = QDateTime::fromTime_t(ts);
        friendly = dt1.toString("yyyy/MM/dd");
    }

    return friendly;

}

CVTabWidget *CVUtil::getTabWidget(QWidget *widget)
{
    while (widget->parentWidget())
    {
        if (qobject_cast<CVTabWidget *>(widget)) {
            return qobject_cast<CVTabWidget *>(widget);
        }
        else
        {
            widget = widget->parentWidget();
        }
    }

    return NULL;
}

QString CVUtil::filterString(QString str)
{
    QString temp = str.replace("&quot;", "\"").replace("&apos;", "'")
            .replace("&nbsp;", " ").replace("&ldquo;", "\"")
            .replace("&rdquo;", "\"").replace("&#39;", "'");
    //QStringList strList = temp.split(QRegExp("[\r\n]"), QString::SkipEmptyParts);
    //return strList.join("<br />");
    return temp;
}

